<?php
require_once("config.class.php");
class Debug{
	static private $_instance;
	static private $debugoutput=array();
	static private $parameters=null;
	static private $config=null;
	public function __construct(){
		//echo "<pre>".self::$parameters['path']."</pre>";
		if(self::$parameters==null){
			self::$config=Config::getInstance();
			self::$parameters=self::$config->getGroup('debug');
		}
	}
	public function output($level,$message,$file="",$line=""){
		//echo "<pre>$message</pre>";
		if(stristr($level,self::$parameters['level'])){
			if($file!=""){$file=";".$file;}
			if($line!=""){$line=";".$line;}
			if(self::$parameters['format']){
				$format=self::$parameters['format'];
				$dateformat=(isset(self::$parameters['dateformat'])&&self::$parameters['dateformat']!=""?self::$parameters['dateformat']:"Y-m-d h:i:s");
				$formatedmessage= str_replace("%date",date("Y-m-d h:i:s"),$format);
				$formatedmessage= str_replace("%message",$message,$formatedmessage);
				$formatedmessage= str_replace("%file",$file,$formatedmessage);
				$formatedmessage= str_replace("%level",$level,$formatedmessage);
				
			}else{
				$formatedmessage=date("Y-m-d h:i:s").";".time().";$level;$message $file $line";
			}
			self::$debugoutput[]="<li>".$formatedmessage."</li>";
			$this->write($formatedmessage);
		}
	}
	public function write($message){
		$file = fopen(self::$config->get('debug','path'),'a+');
		//echo "<pre>".self::$parameters['path']."</pre>";
		if($file){
			fwrite($file,$message."\r\n");
			fclose($file);
		}
	} 
	public function flush($duration=0){
		if($duration!=0){
			$this->output("debug","Page generated in ".($duration*1000)." ms.");
			echo "<div class=\"\">Page generated in ".($duration*1000)." ms.</div>";
		}
		
		if(self::$config->get("debug","show")){
			echo"<div id=\"debug\" title=\"Debug output\"><pre class=\"debug\"><ol>";
			foreach(self::$debugoutput as $message) echo "$message";
			echo "</ol></pre></div>";
		}
	}
	
	public function debugWindow(){
		if(self::$config->get("debug","show")){
			echo "<span class=\"subpanel\"><span class=\"title\">Debug</span>".
				 "<a href=\"javascript:if(\$('#debug').dialog('isOpen')){\$('#debug').dialog('close')}else{\$('#debug').dialog('open')};\">debug</a></span>";

		}
	}
	/**
	 * return the static instance of the THeme Manager.
	 * create the instance if does not already exist.
	 */
	function getInstance(){
		if(!isset(self::$_instance)){
			self::$_instance = new Debug();
		}
		return self::$_instance;
	}
	
}
function __DEBUG_WINDOW(){
	Debug::getInstance()->debugWindow();
}
function __debug($file="",$line="",$message){
	Debug::getInstance()->output("DEBUG",$message,$file,$line);
}
function __info($file="",$line="",$message){
	Debug::getInstance()->output("INFO ",$message,$file,$line);
}
function __warn($file="",$line="",$message){
	Debug::getInstance()->output("WARN ",$message,$file,$line);
}
function __error($file="",$line="",$message){
	Debug::getInstance()->output("ERROR",$message,$file,$line);
}
function __fatal($file="",$line="",$message){
	Debug::getInstance()->output("FATAL",$message,$file,$line);
}
?>